package cn.pping.org.controller;

import cn.pping.basic.utils.AjaxResult;
import cn.pping.basic.utils.PageList;
import cn.pping.org.domain.Shop;
import cn.pping.org.query.ShopQuery;
import cn.pping.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "商铺的API",description="商铺相关的CRUD功能")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /*
     * @Title:
     * @Description: 通过ID查询
     * @Author: Pan
     * @Version: 1.0
     * @Date:  2022/6/24 11:01
     * @Parameters:
     * @Return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Shop loadById(@PathVariable("id") Long id) {
        return shopService.loadById(id);
    }
    /*
    * @Title:
    * @Description: 查寻全部
    * @Author: Pan
    * @Version: 1.0
    * @Date:  2022/6/24 12:40
    * @Parameters:
    * @Return
    */

    @GetMapping
    public List<Shop> loadAll() {
        return shopService.loadAll();
    }

    /*
     * @Title:
     * @Description:高级查询加分页
     * @Author: Pan
     * @Version: 1.0
     * @Date:  2022/6/24 11:01
     * @Parameters:
     * @Return
     */
    @PostMapping
    public PageList<Shop> queryData(@RequestBody ShopQuery shopQuery) {
        PageList<Shop> data = shopService.queryData(shopQuery);
        return data;
    }
    /*
    * @Title:
    * @Description: 根据ID删除
    * @Author: Pan
    * @Version: 1.0
    * @Date:  2022/6/24 12:37
    * @Parameters:
    * @Return
    */

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id) {
        try {
            shopService.remove(id);
            return AjaxResult.getMe();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getMe().setSuccess(false).setMessage("系统繁忙请稍后在式");
        }

    }

    /*
    * @Title:
    * @Description: 修改新增
    * @Author: Pan
    * @Version: 1.0
    * @Date:  2022/6/24 12:43
    * @Parameters:
    * @Return
    */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Shop shop){
        try {
            if(shop.getId() == null){
                shopService.save(shop);
            }else {
                shopService.update(shop);
            }
            return AjaxResult.getMe();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getMe().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
    /**
    * @Title: 批量删除
    * @Description: TODO
    * @Author: Pan
    * @Version: 1.0
    * @Date:  2022/6/26 1:13
    * @Parameters: [ids]
    * @Return cn.pping.basic.utils.AjaxResult
    */

    @PatchMapping
    public AjaxResult batchDepartmant(@RequestBody List<Long> ids){
        try {
           shopService.batch(ids);
           return AjaxResult.getMe();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getMe().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


}
